{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false
   },
   "source": [
    "# USB Wifi Example\n",
    "\n",
    "In this notebook, a wifi dongle has been plugged into the board. Specifically a RALink wifi dongle commonly used with Raspberry Pi kits is connected into the board.  Using Linux calls and Python functions, we will determine the unique name of the dongle and then create a network entry for a known ssid/password pair.  This demo was first done using an iPhone hotspot wireless connection.\n",
    "\n",
    "References <br>\n",
    "http://www.canakit.com/raspberry-pi-wifi.html <br>\n",
    "\n",
    "![](data/wifi.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Create USB WIFI instance\n",
    "Make sure the USB WIFI module has been plugged in, and the base overlay is loaded."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq import Overlay\n",
    "from pynq.drivers import Usb_Wifi\n",
    "\n",
    "Overlay(\"base.bit\").download()\n",
    "port = Usb_Wifi()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Connect to a WIFI link\n",
    "Type in the SSID and password as instructed. It may take a while to establish the connection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "hide_input": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type in the SSID:Visitor\n",
      "Type in the password:47937061\n"
     ]
    }
   ],
   "source": [
    "ssid = input(\"Type in the SSID:\")\n",
    "pwd = input(\"Type in the password:\")\n",
    "port.connect(ssid, pwd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Test connection\n",
    "Ping 10 packets, then the following cell stops automatically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PING xilinx.com (172.19.127.11) 56(84) bytes of data.\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=1 ttl=60 time=0.893 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=2 ttl=60 time=0.544 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=3 ttl=60 time=0.647 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=4 ttl=60 time=0.722 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=5 ttl=60 time=0.532 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=6 ttl=60 time=0.405 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=7 ttl=60 time=0.687 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=8 ttl=60 time=0.341 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=9 ttl=60 time=0.429 ms\n",
      "64 bytes from xsj-pvapcs202.xilinx.com (172.19.127.11): icmp_seq=10 ttl=60 time=0.636 ms\n",
      "\n",
      "--- xilinx.com ping statistics ---\n",
      "10 packets transmitted, 10 received, 0% packet loss, time 8995ms\n",
      "rtt min/avg/max/mdev = 0.341/0.583/0.893/0.160 ms\n"
     ]
    }
   ],
   "source": [
    "! ping xilinx.com -c 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Reset connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "port.reset()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
